package server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import voSetting.LoginVO;
import voSetting.SetMyInfoVO;
import kr.db.MySql;

public class LoginProcess {
	
	private LoginVO vo=null;
	private Connection conn= null;
	
	
	public LoginProcess() {
		conn = new MySql().getConn();
	}

	public boolean logincheck(LoginVO lvo){	// 1. 사용자 정보가 맞는지 체크
		vo = lvo;
		
		String sql = "select m_id, m_pw "
				+ "from semyadmin.messenger "
				+ "where m_id = ? and m_pw = ? ";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getM_id());
			pst.setString(2, vo.getM_pw());
			
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				return true;
			}
			
			rs.close();
			pst.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
		
	}
	
	public SetMyInfoVO find_user_information(LoginVO lvo){
		SetMyInfoVO smvo = null;
		vo = lvo;
		
		String sql = "select e.e_number, e.e_name, e.e_email, e.e_contact, e.e_position, "
				+ "d1.d_tname, d1.d_code, d1.d_pcode, d2.d_tname, m.m_id "
				+ "from semyadmin.employees e "
				+ "inner join semyadmin.departments d1 on d1.d_code = e.d_code "
				+ "inner join semyadmin.departments d2 on d1.d_pcode = d2.d_code "
				+ "inner join semyadmin.messenger m on m.e_number = e.e_number "
				+ " where m.m_id = ? ";
		
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getM_id());
			
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				smvo = new SetMyInfoVO();
				smvo.setE_number(rs.getLong("e.e_number"));
				smvo.setE_name(rs.getString("e.e_name"));
				smvo.setE_email(rs.getString("e.e_email"));
				smvo.setE_contact(rs.getInt("e.e_contact"));
				smvo.setE_position(rs.getString("e_position"));
				smvo.setD_tname(rs.getString("d1.d_tname"));
				smvo.setD_code(rs.getInt("d1.d_code"));
				smvo.setD_pcode(rs.getInt("d1.d_pcode"));
				smvo.setM_id(rs.getString("m.m_id"));
			}
			
			rs.close();
			pst.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(smvo == null){
			System.out.println("smvo에 값이 셋팅되지 않음 !!!  null ");
		}
		return smvo;
	}
}
